import { Component, EventKeyboard, EventMouse, Input, input, KeyCode, Vec3, _decorator } from 'cc';
const { ccclass, property } = _decorator;

export const INPUT = {
    LEFT: 0,
    RIGHT: 0,
    UP: 0,
    DOWN: 0,
    FOWARD: 0,
    BACKWARD: 0,

    ZOOM: 0,

    ROTATE: 0,
    ROTATE_PARAMS: { x: 0, y: 0 },
};

@ccclass('CameraCtrl')
export class CameraCtrl extends Component {
    /* -- PROPERTIES -- */

    @property
    public speed = 100; // 平移速度

    @property
    public rotateSpeed = 10; // 旋转速度

    @property
    public zoomSpeed = 0.05; // 镜头缩放速度

    private velocity = new Vec3();
    private rotateVelocity = new Vec3();

    /* -- LIFE CYCLE -- */

    public onLoad() {
        this.addListener();
    }

    public start() {}

    public update(dt: number) {
        this.cameraMove(dt);
    }

    public onDestroy() {
        this.removeListener();
    }

    /* -- METHODS -- */
    public getVelocaity() {
        return this.velocity;
    }

    public addListener() {
        input.on(Input.EventType.KEY_DOWN, this.onKeyDown, this);
        input.on(Input.EventType.KEY_UP, this.onKeyUp, this);

        input.on(Input.EventType.MOUSE_MOVE, this.onMouseMove, this);
        input.on(Input.EventType.MOUSE_DOWN, this.onMouseDown, this);
        input.on(Input.EventType.MOUSE_UP, this.onMouseUp, this);
        input.on(Input.EventType.MOUSE_WHEEL, this.onMouseWheel, this);
    }

    public removeListener() {
        input.off(Input.EventType.KEY_DOWN, this.onKeyDown, this);
        input.off(Input.EventType.KEY_UP, this.onKeyUp, this);

        input.off(Input.EventType.MOUSE_MOVE, this.onMouseMove, this);
        input.off(Input.EventType.MOUSE_DOWN, this.onMouseDown, this);
        input.off(Input.EventType.MOUSE_UP, this.onMouseUp, this);
        input.off(Input.EventType.MOUSE_WHEEL, this.onMouseWheel, this);
    }

    private onKeyDown(event: EventKeyboard) {
        switch (event.keyCode) {
            case KeyCode.KEY_A:
                INPUT.LEFT = 1;
                break;
            case KeyCode.KEY_D:
                INPUT.RIGHT = 1;
                break;
            case KeyCode.KEY_W:
                INPUT.FOWARD = 1;
                break;
            case KeyCode.KEY_S:
                INPUT.BACKWARD = 1;
                break;
            case KeyCode.SPACE:
            case KeyCode.KEY_E:
                INPUT.UP = 1;
                break;
            case KeyCode.CTRL_LEFT:
            case KeyCode.KEY_Q:
                INPUT.DOWN = 1;
                break;
        }
    }

    private onKeyUp(event: EventKeyboard) {
        switch (event.keyCode) {
            case KeyCode.KEY_A:
                INPUT.LEFT = 0;
                break;
            case KeyCode.KEY_D:
                INPUT.RIGHT = 0;
                break;
            case KeyCode.KEY_W:
                INPUT.FOWARD = 0;
                break;
            case KeyCode.KEY_S:
                INPUT.BACKWARD = 0;
                break;
            case KeyCode.SPACE:
            case KeyCode.KEY_E:
                INPUT.UP = 0;
                break;
            case KeyCode.CTRL_LEFT:
            case KeyCode.KEY_Q:
                INPUT.DOWN = 0;
                break;
        }
    }

    private onMouseMove(event: EventMouse) {
        if (INPUT.ROTATE === 1) {
            INPUT.ROTATE_PARAMS.x = event.movementX;
            INPUT.ROTATE_PARAMS.y = event.movementY;
        }
    }

    private onMouseDown(event: EventMouse) {
        switch (event.getButton()) {
            case EventMouse.BUTTON_RIGHT:
                INPUT.ROTATE = 1;
                break;
        }
    }

    private onMouseUp(event: EventMouse) {
        switch (event.getButton()) {
            case EventMouse.BUTTON_RIGHT:
                INPUT.ROTATE = 0;
                break;
        }
    }

    private onMouseWheel(event: EventMouse) {
        INPUT.ZOOM = event.getScrollY();
    }

    private cameraMove(dt: number) {
        this.velocity.set(0, 0, 0);

        if (INPUT.LEFT === 1) {
            this.velocity.subtract(this.node.right);
        }

        if (INPUT.RIGHT === 1) {
            this.velocity.add(this.node.right);
        }

        if (INPUT.FOWARD === 1) {
            this.velocity.add(this.node.forward);
        }

        if (INPUT.BACKWARD === 1) {
            this.velocity.subtract(this.node.forward);
        }

        if (INPUT.UP === 1) {
            this.velocity.add(Vec3.UP);
        }

        if (INPUT.DOWN === 1) {
            this.velocity.subtract(Vec3.UP);
        }

        if (INPUT.ZOOM !== 0) {
            Vec3.multiplyScalar(this.velocity, this.node.forward, INPUT.ZOOM * this.zoomSpeed);
            INPUT.ZOOM = 0;
        }

        const newPos = this.node.worldPosition.add(this.velocity.multiplyScalar(this.speed * dt));
        this.node.setWorldPosition(newPos);

        if (INPUT.ROTATE === 1) {
            const eulerAngles = this.node.eulerAngles;
            this.rotateVelocity.set(-INPUT.ROTATE_PARAMS.y, -INPUT.ROTATE_PARAMS.x, 0);
            this.node.setRotationFromEuler(eulerAngles.add(this.rotateVelocity.multiplyScalar(this.rotateSpeed * dt)));

            INPUT.ROTATE_PARAMS.x = 0;
            INPUT.ROTATE_PARAMS.y = 0;
        }
    }
}


export const ShuiWenPointConfig = [
    // 地面上
    {
        ShuiWenPointID: '101',
        ShuiWenPointLocation: [2198.589, -1985.973, -149.635],
        deviceName: 'S20364',
        WaterLevel: 9,
        WaterVolume: 30,
        State: 0,
    },
    {
        ShuiWenPointID: '102',
        ShuiWenPointLocation: [1773.495, -1985.963, 151.98],
        deviceName: 'S20355',
        WaterLevel: 90,
        WaterVolume: 100,
        State: 1,
    },
    {
        ShuiWenPointID: '103',
        ShuiWenPointLocation: [825.912, -1985.963, 219.927],
        deviceName: 'S20312',
        WaterLevel: 60,
        WaterVolume: 70,
        State: 1,
    },
    {
        ShuiWenPointID: '104',
        ShuiWenPointLocation: [1771.691, -1985.963, 296.564],
        deviceName: 'S2036437',
        WaterLevel: 60,
        WaterVolume: 100,
        State: 1,
    },
];

export const JueJinPointConfig = [
    {
        JJMId: '1',
        JJMName: '4034上09运顺',
        JJMLocation: [-3056.96, -1210.496, -945],
        JinChi: 50,
        Worker: 8,
        State: 0,
    },
    {
        JJMd: '2',
        JJMName: '40203运顺',
        JJMLocation: [-411.605, -1932.924, -889.109],
        JinChi: 210,
        Worker: 8,
        State: 0,
    },
    {
        JJMd: '3',
        JJMName: '4034上02云顺',
        JJMLocation: [-2011.025, -1144.702, 820.027],
        JinChi: 211,
        Worker: 8,
        State: 0,
    },
    {
        JJMd: '4',
        JJMName: '402采区南翼区域...',
        JJMLocation: [30, 0, 10],
        JinChi: 110,
        Worker: 8,
        State: 0,
    },
    {
        JJMd: '5',
        JJMName: '41202运顺',
        JJMLocation: [-361.049, -1202.805, 888.125],
        JinChi: 230,
        Worker: 8,
        State: 0,
    },
    {
        JJMd: '6',
        JJMName: '402采区2#水仓',
        JJMLocation: [50, 0, 10],
        JinChi: 240,
        Worker: 8,
        State: 0,
    },
    {
        JJMd: '7',
        JJMName: '4034上02切眼',
        JJMLocation: [60, 0, 10],
        JinChi: 230,
        Worker: 8,
        State: 0,
    },
];

export const ZCMPointConfig = [
    {
        ZCMId: '41109',
        ZCMName: '41109综采面',
        ZCMLocation: [507,-1191,930],
        OutPut: 168,
        Worker: 8,
        State: 0,
    },
    {
        ZCMId: '40204',
        ZCMName: '40204综采面',
        ZCMLocation: [-29, -1989, 1298],
        OutPut: 178,
        Worker: 8,
        State: 0,
    },
];



let MinePressureConfigData = [
    {
        id: '1011',
        name: '锚索-1011',
        pressure: 450,
        state: 0,
        position: new Vec3(2074.748, -1985.973, 156.673),
    },
    { id: '1012', name: '锚索-1012', pressure: 150, state: 1, position: new Vec3(2151.111, -1985.973, 87.514) },
    { id: '1013', name: '锚索-1013', pressure: 50, state: 0, position: new Vec3(2150.329, -1985.963, 251.98) },
];


const config = [
    { id: '1', name: '主斜井机尾落煤点', position: new Vec3(2091.968, -1862.788, -1820.358) },
    { id: '2', name: '4煤胶带大巷一部机头', position: new Vec3(2091.968, -1904.275, -1780.094) },
    { id: '3', name: '大巷二部机头落煤点', position: new Vec3(2111.196, -1992.298, 261.414) },
    { id: '4', name: '101皮带机头', position: new Vec3(2136.595, -1196.466, 217.018) }, // 用的西翼一部皮带机头位置
    { id: '5', name: '西部给煤机平台', position: new Vec3(2150.357, -1992.298, 212.018) },
];